﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Heaven.Texturing
{
    /// <summary>
    /// Represents 2-dimentional texture with mipmap levels
    /// </summary>
    public class PlanarTexture : Texture
    {
        #region Fields

        // The main layer of the texture
        PlanarLayer layer;
        // Mipmap levels
        PlanarLayer[] mipmaps;

        #endregion

        #region Properties

        /// <summary>
        /// The main layer of the texture
        /// </summary>
        public PlanarLayer Layer
        {
            get { return layer; }
            set 
            {
                UnsubscribeEvents();
                layer = value;
                SubscribeEvents();
                RaiseChangedEvent();
            }
        }

        /// <summary>
        /// Mipmap levels
        /// </summary>
        public PlanarLayer[] Mipmaps
        {
            get { return mipmaps; }
            set 
            {
                UnsubscribeEvents();
                mipmaps = value;
                SubscribeEvents();
                RaiseChangedEvent();
            }
        }

        /// <summary>
        /// Format of the texture (the main layer 
        /// and mipmaps must have the same format)
        /// </summary>
        public LayerFormat Format
        {
            get { return layer.Format; }
        }

        /// <summary>
        /// Width of the layer
        /// </summary>
        public int Width
        {
            get { return layer.Width; }
        }

        /// <summary>
        /// Height of the layer
        /// </summary>
        public int Height
        {
            get { return layer.Height; }
        }

        #endregion

        #region Initialization

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="layer">The main layer of the texture</param>
        /// <param name="mipmaps">Mipmap levels</param>
        public PlanarTexture(PlanarLayer layer, PlanarLayer[] mipmaps) : base()
        {
            this.layer = layer;
            this.mipmaps = mipmaps;
            SubscribeEvents();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="layer">The main layer of the texture</param>
        public PlanarTexture(PlanarLayer layer) : this(layer, new PlanarLayer[] {})
        {
        }

        #endregion

        #region Routine

        void SubscribeEvents()
        {
            layer.Changed += new EventHandler(OnLayerChanged);
            foreach (PlanarLayer textureLayer in mipmaps)
            {
                textureLayer.Changed += new EventHandler(OnLayerChanged);
            }
        }

        void UnsubscribeEvents()
        {
            layer.Changed -= new EventHandler(OnLayerChanged);
            foreach (PlanarLayer textureLayer in mipmaps)
            {
                textureLayer.Changed -= new EventHandler(OnLayerChanged);
            }
        }

        void OnLayerChanged(object sender, EventArgs e)
        {
            RaiseChangedEvent();
        }

        #endregion
    }
}
